package mobi.yuanshen.common.utils;


import cn.hutool.crypto.digest.DigestUtil;

import java.util.Random;

public class SaltCodeUtils {

 public static final String VERIFY_CODES
         = "0bm1a2ncdefi34g5hg678kl9Au$Bt&C*sDEry#FGHz@IJoK-LMpNqsPQ+wRSTUVWvXYZ";
 private static Random random = new Random();

 /**
     * 使用系统默认字符源生成盐码
     * @param verifySize    盐码长度
     * @return
     */
    public static String generateSaltCode(int verifySize){
        return generateVerifyCode(verifySize, VERIFY_CODES);
    }
    /**
     * 使用指定源生成盐码
     * @param verifySize    盐码长度
     * @param sources   盐码字符源
     * @return
     */
    public static String generateVerifyCode(int verifySize, String sources){
        if(sources == null || sources.length() == 0){
            sources = VERIFY_CODES;
        }
        int codesLen = sources.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(verifySize);
        for(int i = 0; i < verifySize; i++){
            verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
        }
        return verifyCode.toString();
    }


    /**
     * 获取MD5密码
     *
     * @param password
     * @param username
     * @param salt     盐值
     * @return
     */
    public static String getMdtPassword(String password,
                                  String username,
                                  String salt) {
        for (int i = 0; i < 3; i++) {
            String newPs = salt + password + username;
            password = DigestUtil.md5Hex(newPs);
        }
        return password;
    }
}